package com.myk.game.gobangchess.ai.gravityfour;

public class scoreWeighting {
    
    public static int scoreWeighting1(int[][] board, int turn) {
        int[][] convKernel = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}};

        int[][] modifiedBoard = new int[board.length][board[0].length];
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if(board[i][j] == -turn){
                    modifiedBoard[i][j] = -3*turn;
                }else{
                    modifiedBoard[i][j] = board[i][j];
                }
            }
        }
        int[][] convResult = conv2(modifiedBoard, convKernel);
        int[][] absConvResult = new int[convResult.length][convResult[0].length];
        for (int i = 0; i < convResult.length; i++) {
            for (int j = 0; j < convResult[0].length; j++) {
                absConvResult[i][j] = Math.abs(convResult[i][j]);
            }
        }

        int[][] scoreMatrix1 = new int[convResult.length][convResult[0].length];
        for (int i = 0; i < convResult.length; i++) {
            for (int j = 0; j < convResult[0].length; j++) {
                scoreMatrix1[i][j] = convResult[i][j] * absConvResult[i][j];
            }
        }

        int scoreCrowding = 0;
        for (int i = 0; i < scoreMatrix1.length; i++) {
            for (int j = 0; j < scoreMatrix1[0].length; j++) {
                scoreCrowding += scoreMatrix1[i][j];
            }
        }

        int[][] convKernel1 = {{1, 1}, {1, 1}};

        int[][] convResult1 = conv2(modifiedBoard, convKernel1);
        int[][] absConvResult1 = new int[convResult1.length][convResult1[0].length];
        for (int i = 0; i < convResult1.length; i++) {
            for (int j = 0; j < convResult1[0].length; j++) {
                absConvResult1[i][j] = Math.abs(convResult1[i][j]);
            }
        }

        int[][] scoreMatrix11 = new int[convResult1.length][convResult1[0].length];
        for (int i = 0; i < convResult1.length; i++) {
            for (int j = 0; j < convResult1[0].length; j++) {
                scoreMatrix11[i][j] = convResult1[i][j] * absConvResult1[i][j];
            }
        }

        int scoreCrowding1 = 0;
        for (int i = 0; i < scoreMatrix11.length; i++) {
            for (int j = 0; j < scoreMatrix11[0].length; j++) {
                scoreCrowding1 += scoreMatrix11[i][j];
            }
        }

        int[][] centerCheck = {{1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1},
                {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}};

        int[][] scoreMatrix2 = new int[centerCheck.length][centerCheck[0].length];
        for (int i = 0; i < centerCheck.length; i++) {
            for (int j = 0; j < centerCheck[0].length; j++) {
                scoreMatrix2[i][j] = centerCheck[i][j] * board[i][j];
            }
        }

        int scoreNearCenter = 0;
        for (int i = 0; i < scoreMatrix2.length; i++) {
            for (int j = 0; j < scoreMatrix2[0].length; j++) {
                scoreNearCenter += 1800 * scoreMatrix2[i][j];
            }
        }

        return scoreNearCenter + scoreCrowding + 2*scoreCrowding1;
    }

    public static int[][] conv2(int[][] matrix1, int[][] matrix2) {
        int m = matrix1.length;
        int n = matrix1[0].length;
        int p = matrix2.length;
        int q = matrix2[0].length;
        int r = m - p + 1;
        int s = n - q + 1;
        
        int[][] result = new int[r][s];

        for (int i = 0; i < r; i++) {
            for (int j = 0; j < s; j++) {
                for (int k = 0; k < p; k++) {
                    for (int l = 0; l < q; l++) {
                        result[i][j] += matrix1[i + k][j + l] * matrix2[k][l];
                    }
                }
            }
        }

        return result;
    }
}

